﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;

namespace ImbedLibrary
{
    public class JsonHelper
    {
        public static String StringFormat(String str, Type type)
        {
            if (type == typeof(String))
            {
                str = StringFilter(str);
                str = "\"" + str + "\"";
            }
            else if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
            else if (type == typeof(Guid))
            {
                str = "\"" + str + "\"";
            }
            else if (type != typeof(String) || String.IsNullOrEmpty(str))
            {
                str = "\"" + str + "\"";
            }
            return str;
        }

        public static String StringFilter(String str)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                char c = str.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }

        public static String ListToJson<T>(IList<T> list)
        {
            object obj = list[0];
            return ListToJson<T>(list, obj.GetType().Name);
        }


        public static String ListToJasonOrderWebSite<T, B>(IList<T> list, IList<B> list1)
        {
            StringBuilder json = new StringBuilder();
            if (list != null)
            {
                T obj1 = Activator.CreateInstance<T>();

                PropertyInfo[] pi1 = obj1.GetType().GetProperties();

                json.Append("{[{");
                for (int i = 0; i < pi1.Length; i++)
                {
                    if (pi1[i].GetValue(list[0], null) != null)
                    {
                        Type type = pi1[i].GetValue(list[0], null).GetType();
                        json.Append(pi1[i].Name.ToString() + ":" + StringFormat(pi1[i].GetValue(list[0], null).ToString(), type));
                        if (i < pi1.Length - 1)
                        {
                            json.Append(",");
                        }
                    }
                    else
                    {
                        json.Append("\"" + pi1[i].Name.ToString() + "\":\"\"");
                        if (i + 1 != pi1.Length) { json.Append(","); }
                    }
                }
                json.Append(",hostCount:" + list1.Count);
                if (list1.Count > 0)
                {
                    json.Append(",");
                }
                for (int i = 0; i < list1.Count; i++)
                {
                    B obj2 = Activator.CreateInstance<B>();
                    PropertyInfo[] pi2 = obj2.GetType().GetProperties();
                    for (int j = 0; j < pi2.Length; j++)
                    {
                        if (pi2[j].GetValue(list1[i], null) != null)
                        {
                            Type type = pi2[j].GetValue(list1[i], null).GetType();
                            json.Append(pi2[j].Name.ToString() + i + ":" + StringFormat(pi2[j].GetValue(list1[i], null).ToString(), type));
                            json.Append(",");
                            if (i > list1.Count - 1)
                            {
                                json.Remove(json.Length - 1, 1);
                            }
                        }
                        else
                        {
                            json.Append(pi2[j].Name.ToString() + ":\"\"");
                            if (j + 1 != pi2.Length) { json.Append(","); }
                        }
                    }

                }
                json.Append("}]}");
            }
            return json.ToString();
        }


        /// <summary>
        /// 对象集合转Json数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="JsonName"></param>
        /// <returns></returns>
        private static String ListToJson<T>(IList<T> list, String JsonName)
        {
            StringBuilder json = new StringBuilder();
            if (String.IsNullOrEmpty(JsonName))
                JsonName = list[0].GetType().Name;
            //json.Append("{total:");
            //json.Append(list.Count + ",rows:[");
            json.Append("[");
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    PropertyInfo[] pi = obj.GetType().GetProperties();
                    json.Append("{");
                    for (int j = 0; j < pi.Length; j++)
                    {
                        if (pi[j].GetValue(list[i], null) != null)
                        {
                            Type type = pi[j].GetValue(list[i], null).GetType();
                            json.Append(pi[j].Name.ToString() + ":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type));
                            if (j < pi.Length - 1)
                            {
                                json.Append(",");
                            }
                        }
                        else
                        {
                            json.Append(pi[j].Name.ToString() + ":\"\"");
                            if (j + 1 != pi.Length) { json.Append(","); }
                        }
                    }
                    json.Append("}");
                    if (i < list.Count - 1)
                    {
                        json.Append(",");
                    }
                }
            }
            json.Append("]");
            return json.ToString();
        }


    }
}
